<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible" width="80%">
    <ContentWrap>
      <el-form
        class="-mb-15px"
        :model="formData"
        ref="formRef"
        :inline="true"
        label-width="110px"
        v-loading="formLoading"
      >
        <el-form-item label="用户名称" prop="nickname">
          <el-input v-model="formData.nickname" clearable @keyup.enter="handleQuery" class="!w-140px" />
        </el-form-item>
        <el-form-item>
          <el-button @click="handleQuery" type="primary">搜索</el-button>
          <el-button @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </ContentWrap>
    <!-- 列表 -->
    <ContentWrap>
      <el-table
        ref="multipleTableRef"
        border
        row-key="id"
        :height="tableHeight"
        v-loading="loading"
        :data="list"
        @select-all="handleCheckBox"
        @select="handleCheckBox"
      >
        <el-table-column align="center" type="selection" />
        <el-table-column label="字段名称" prop="nickname" />
      </el-table>
    </ContentWrap>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
  import * as AbnormalApi from '@/api/config/exceptionManagement/abnormal'
  const dialogVisible = ref(false) // 弹窗的是否展示
  const dialogTitle = ref('') // 弹窗的标题
  const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
  const multipleTableRef = ref()
  const formData = ref({
    nickname: ''
  })
  const list = ref<any>([]) // 列表的数据
  const passData = ref<any>([])
  const formRef = ref() // 表单 Ref

  /** 打开弹窗 */
  const open = async (list) => {
    console.log(list)
    passData.value = list
    selectList.value = list
    dialogVisible.value = true
    getCustomerProfileList()
  }
  defineExpose({ open }) // 提供 open 方法，用于打开弹窗

  /** 提交表单 */
  const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
  const submitForm = async () => {
    let params = {
      list: selectList.value
    }
    // 校验表单
    emit('success', params)
    dialogVisible.value = false
  }
  //重置按钮
  const resetQuery = () => {
    formData.value.name= ''
    handleQuery()
  }
  /** 搜索按钮操作 */
  const handleQuery = () => {
    getCustomerProfileList()
  }
  const loading = ref(true) // 列表的加载中
  //获取列表
  const getCustomerProfileList = async () => {
    loading.value = true
    try {
      const data = await AbnormalApi.getUserList(formData.value.nickname)
      list.value = data
      nextTick(() => {
        for (let i = 0; i < list.value.length; i++) {
          for (let j = 0; j < passData.value.length; j++) {
            if (
              list.value[i].id == passData.value[j].id &&
              list.value[i].id != null
            ) {
              multipleTableRef.value.toggleRowSelection(list.value[i], true)
            }
          }
        }
      })
    } finally {
      loading.value = false
    }
  }

  /**选中 */
  const selectList = ref<any>([])
  //单选
  const handleCheckBox = (row) => {
    selectList.value = selectList.value
      .filter((item) => {
        return !row.map((item) => item.id).includes(item.id)
      })
      .concat(row)
    const newArr = ref<any>([])
    //获取本页未选中的数据
    newArr.value = list.value.filter((item) => !row.some((ele) => ele.id === item.id))
    let news = ref<any>([])
    if (newArr.value.length) {
      const arrs = newArr.value
      for (let i = 0; i < selectList.value.length; i++) {
        let flag = ref(true)
        for (let j = 0; j < arrs.length; j++) {
          if (selectList.value[i].id === arrs[j].id) {
            flag.value = false
            break
          }
        }
        if (flag.value) {
          news.value.push(selectList.value[i])
        }
      }
      selectList.value = news.value
    }
  }
</script>
